在 Go 語言中, 集合 本質上是相同類型的項目群組,以實現高效存取。『貨物艙』代表我們需要將大量資料統一管理於單一識別符之下,而非使用多個獨立變數。
1. 組合文字常量
一個 組合文字常量 是一種簡潔語法,可用來以您所需的值初始化任何組合型別。它允許您使用以下語法,在一步之中宣告並初始化陣列: type{value1, value2, ...}。
2. 零起始索引
陣列的索引從 0 開始。包含 8 個行星的集合,需透過索引 0 到 7 來存取。若存取超出此範圍的索引,將導致編譯錯誤或執行時期崩潰。
圖 16.1:索引為 0-7 的行星
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
What is the primary purpose of a composite literal in Go?
To perform complex mathematical calculations on groups of numbers.
A concise syntax to declare and initialize a composite type with values.
To convert a slice into an array automatically.
To delete elements from a collection.
✅ Correct!
Correct! It allows you to define the structure and the data in one step.❌ Incorrect
Composite literals are for initialization, not calculation or deletion.QUESTION 2
If an array has 8 elements (as in Figure 16.1), what is the index of the first item?
1
-1
0
8
✅ Correct!
Correct. Go uses zero-based indexing for all collections.❌ Incorrect
Collections in Go always start at index 0.QUESTION 3
What does the ellipsis (...) in a function declaration typically indicate?
The function is incomplete.
The function accepts a variable number of arguments (variadic).
The function will loop infinitely.
The function returns a pointer.
✅ Correct!
Yes, the ellipsis denotes variadic parameters.❌ Incorrect
The ellipsis is used for variadic functions, allowing them to take any number of values.QUESTION 4
In the context of the Go Cargo Bay, what are 'collections'?
Variables that can only store numbers.
Just groups of things, typically of the same type.
Special functions that sort data.
Hardware components used for storage.
✅ Correct!
Exactly. Arrays, slices, and maps are all types of collections.❌ Incorrect
Collections are logical groupings of data within the code.QUESTION 5
What happens if you attempt to access planets[8] in an array of size 8?
It returns the first element again (wrap around).
It returns a null or empty string.
It causes a compile-time error or a runtime panic.
It automatically expands the array to size 9.
✅ Correct!
Correct! Array sizes are fixed and bounds are strictly enforced.❌ Incorrect
Arrays in Go are fixed-length; you cannot access an index equal to or greater than the length.Module Challenge: The Ship's Manifest
Representing physical items as digital arrays.
You are organizing a spaceship's inventory. You have a collection of personal items like stamps, books, and trophies. You need to initialize these in Go and perform operations on them.
Q
1. Arrays are for collecting many of the same type of thing. What collections from your own life (e.g., stamps, shoes) could you represent with an array? Explain your choice.
Solution:
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Answers will vary. For example: 'A collection of 10 pairs of shoes could be represented as [10]string because shoes are of the same type (footwear) and the count is fixed in my closet.'
Q
2. Provide the syntax for a 'Step' function signature that takes two Universe types, intended for simulation operations.
Solution:
The signature is:
The signature is:
func Step(a, b Universe). This allows the function to operate on two collections, often used for double-buffering in simulations like the Game of Life.